package service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        try {
            generateImg("三团", "/root/javaProjects/test1/p1.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void generateImg(String name, String path) throws IOException {
        File file = new File(path);
        int width = 400;
        int height = 400;
        int nameLen = name.length();
        String nameWritten;
        if (nameLen <= 2) {
            nameWritten = name;
        } else {
            nameWritten = name.substring(0, 2);
        }
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

        g2.setBackground(getRandomColor());
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.WHITE);
        Font font = null;
        if(nameWritten.length() >= 2) {
            font = new Font("黑体", Font.BOLD, 100);
            g2.setFont(font);
            String firstWritten = nameWritten.substring(0, 1);
            String secondWritten = nameWritten.substring(1, 2);
            if (isChinese(firstWritten) && isChinese(secondWritten)) {
                g2.drawString(nameWritten, 100, 240);
            }
            else if (isChinese(firstWritten) && !isChinese(secondWritten)
                    || !isChinese(firstWritten) && isChinese(secondWritten)) {
                g2.drawString(nameWritten, 118, 240);
            } else {
                font = new Font("微软雅黑", Font.BOLD, 100);
                g2.setFont(font);
                nameWritten = nameWritten.toUpperCase();
                g2.drawString(nameWritten, 125, 240);
            }
        }
        if(nameWritten.length() ==1) {
            if(isChinese(nameWritten)) {
                font = new Font("黑体", Font.BOLD, 100);
                g2.setFont(font);
                g2.drawString(nameWritten, 138, 240);
            }
            else {
                font = new Font("微软雅黑", Font.BOLD, 120);
                g2.setFont(font);
                g2.drawString(nameWritten.toUpperCase(), 145, 240);
            }
        }
        ImageIO.write(bi, "png", file);
    }
    public static boolean isChinese(String str) {
        String regEx = "[\\u4e00-\\u9fa5]+";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        if (m.find())
            return true;
        else
            return false;
    }
    private static Color getRandomColor() {
        String[] beautifulColors =
                new String[]{"0,0,0", "50,72,121","248,98,118","250,41,122","237,84,112"};
        int len = beautifulColors.length;
        Random random = new Random();
        String[] color = beautifulColors[random.nextInt(len)].split(",");
        return new Color(Integer.parseInt(color[0]), Integer.parseInt(color[1]),
                Integer.parseInt(color[2]));
    }
}
